package com.kenny.mypopupwindow;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private Button btn1, btn2;
	private QuickAction quick;
	private PopupWindow window;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn1.setOnClickListener(btn1_Click);
		btn2.setOnClickListener(btn2_Click);
		ActionItem itm1 = new ActionItem(1, "Item1", getResources().getDrawable(R.drawable.ic_folder));
		ActionItem itm2 = new ActionItem(2, "Item2", null);
		quick = new QuickAction(this, QuickAction.VERTICAL);
		quick.addActionItem(itm1);
		quick.addActionItem(itm2);
		quick.setOnActionItemClickListener(quick_Clicked);
	}
	
	private OnClickListener btn1_Click = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			quick.show(v);
		}
		
	};
	
	private OnClickListener btn2_Click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			 LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			  View layout = inflater.inflate(R.layout.popup, null);
			  window = new PopupWindow(layout, 300, 300, true);
			  window.showAtLocation(layout,17, 0, 0);
			  Button btn = (Button) layout.findViewById(R.id.buttonclose);
			  btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					window.dismiss();
				}
				  
			  });
		}
		
		
	};
	
private QuickAction.OnActionItemClickListener quick_Clicked = new QuickAction.OnActionItemClickListener() {
		
		@Override
		public void onItemClick(QuickAction source, int pos, int actionId) {
			if (actionId == 1) {
				Toast.makeText(MainActivity.this, "Item 1 was clicked", Toast.LENGTH_LONG).show();
			} else if (actionId == 2) {
				Toast.makeText(MainActivity.this, "Item 2 was clicked", Toast.LENGTH_LONG).show();
			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
